package com.ajx.blog.service.impl;

import java.util.List;

import com.ajx.blog.entity.Admin;
import com.ajx.blog.entity.ArticleDistribute;
import com.ajx.blog.entity.RolesAdmins;
import com.ajx.blog.mapper.AdminMapper;
import com.ajx.blog.mapper.ArticleDistributeMapper;
import com.ajx.blog.mapper.RolesAdminsMapper;
import com.ajx.blog.service.api.AdminService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private RolesAdminsMapper rolesAdminsMapper;

    @Autowired
    private ArticleDistributeMapper articleDistributeMapper;

    /**
     * @param {Admin} admin 管理员对象
     * @return {boolean} 添加是否成功 true/false
     * 
     * @description: 添加一位管理员
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean insertOneAdmin(Admin admin) {
        if (StringUtils.isNotBlank(admin.getAdminName()) && StringUtils.isNotBlank(admin.getAdminPass())) {
            return adminMapper.insert(admin) > 0;
        } else {
            return false;
        }
    }

    /**
     * @param {Integer} adminId 管理员ID
     * @return {boolean} 删除是否成功 true/false
     * 
     * @description: 通过管理员ID删除一位管理员
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean deleteOneAdmin(Integer adminId) {
        // 如果是文章审核员,审核任务还没有完成,则不允许删除
        QueryWrapper<ArticleDistribute> articleDistributeQW = new QueryWrapper<>();
        articleDistributeQW.eq("admin_id", adminId);
        articleDistributeQW.eq("astatus", "-1");
        if (articleDistributeMapper.selectOne(articleDistributeQW) != null) {
            return false;
        }
        // 管理员-角色关联表删除
        UpdateWrapper<RolesAdmins> rolesAdminsUW = new UpdateWrapper<>();
        rolesAdminsUW.eq("admins", adminId);
        rolesAdminsMapper.delete(rolesAdminsUW);
        // 管理员表删除
        adminMapper.deleteById(adminId);
        return true;
    }

    /**
     * @param {Integer}   aid 管理员id
     * @param {Integer[]} ids 角色列表
     * @return {boolean} 更新是否成功 true/false
     * 
     * @description: 更新管理员的角色
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean updateRolesAdminById(Integer rid, Integer[] ids) {
        if (rid != null) {
            adminMapper.deleteRolesAdminById(rid);
            if (ids[0] != -1) {
                for (int i = 0; i < ids.length; i++) {
                    adminMapper.insertRolesAdmin(ids[i], rid);
                }
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param {String} aname 管理员账号
     * @return {Admin} 管理员对象
     * 
     * @description: 通过管理员账号查找管理员
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Admin findAdminByName(String aname) {
        if (StringUtils.isNotBlank(aname)) {
            QueryWrapper<Admin> qw = new QueryWrapper<>();
            qw.eq("a_name", aname);
            return adminMapper.selectOne(qw);
        } else {
            return null;
        }
    }

    /**
     * @param {Integer} page 当前页码数
     * @return {PageInfo<Admin>} 封装的分页信息对象
     * 
     * @description: 分页查找所有管理员
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public PageInfo<Admin> findAllAdminList(Integer page) {
        if (page != null) {
            QueryWrapper<Admin> qw = new QueryWrapper<>();
            PageHelper.startPage(page, 5);
            List<Admin> adminList = adminMapper.selectList(qw);
            PageInfo<Admin> info = new PageInfo<Admin>(adminList);
            return info;
        } else {
            return null;
        }
    }

}
